package com.shujia.four;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GenericTest {

    @Test
    public void test4() {
        GenericDemo3<String> demo = new GenericDemo3<>();
        demo.run("abc");

        demo.show(new Dog());
        // demo.show(111);
    }

    @Test
    public void test3() {
        // 必须使用 Animal类型或者是Animal的子类
        GenericDemo2<Animal> demo = new GenericDemo2<>();
        Animal animal = new Animal();
        animal.setName("小花");
        demo.setE(animal);

        demo.show();

        GenericDemo2<Dog> demo1 = new GenericDemo2<>();
        demo1.setE(new Dog());
    }

    @Test
    public void test2() {
        // Java 7调用构造器对象时构造器后面的泛型可以省略，Java会根据定义的类型自动推断
//        ArrayList<String> list = new ArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("abc");
        list.add("def");
        list.add("ghi");
//        list.add(1);
//        list.add(3.15);

        Iterator<String> iterable = list.iterator();


        while (iterable.hasNext()) {
            // 不需要强转, 泛型已经执行了具体的数据类型, 对于当前集合来说就是String
            String str = iterable.next();
            System.out.println(str);
        }

    }


    @Test
    public void test1() {
        // 泛型只对当前对象生效
        GenericDemo1<String, Integer> demo = new GenericDemo1<>();

        demo.setE("aaa");
        String str = demo.getE();
        System.out.println(str);
        demo.show();

        GenericDemo1<Integer, Double> demo1 = new GenericDemo1<>();
        demo1.setE(11);


    }
}
